package vip.ola.manage.code.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ola.core.common.annotation.MethodLog;
import vip.ola.core.common.constant.RetEnum;
import vip.ola.core.common.domain.BizResponse;
import vip.ola.core.common.domain.PayResponse;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.entity.code.CodeAccountHistory;
import vip.ola.manage.common.ctrl.BaseController;
import vip.ola.manage.common.service.RpcCommonService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

import static vip.ola.core.common.constant.Constant.MGR_CONTROLLER_ROOT_PATH;

@RestController
@RequestMapping(MGR_CONTROLLER_ROOT_PATH + "/code/accountHistory")
public class AccountHistoryController extends BaseController {

    private final MyLog log = MyLog.getLog(AccountHistoryController.class);

    @Autowired
    private RpcCommonService rpcService;

    @RequestMapping("/list")
    public ResponseEntity<?> list(CodeAccountHistory history, LocalDateTime startTime, LocalDateTime endTime) {
        history.setCreateTime(startTime);
        history.setUpdateTime(endTime);
        IPage<CodeAccountHistory> page = rpcService.codeAccountHistoryService.list(getPageIndex(), getPageSize(), history);
        return ResponseEntity.ok(PayResponse.buildPageSuccess(page));
    }

    @RequestMapping("/count")
    public ResponseEntity<?> count(CodeAccountHistory history, LocalDateTime startTime, LocalDateTime endTime) {
        history.setCreateTime(startTime);
        history.setUpdateTime(endTime);
        Map countMap = rpcService.codeAccountHistoryService.countAll(history);
        Map addMap = rpcService.codeAccountHistoryService.countAdd(history);
        Map subtractMap = rpcService.codeAccountHistoryService.countSubtract(history);

        BigDecimal addTotalAmount = Optional.ofNullable(addMap).map(m -> m.get("totalAmount")).map(Object::toString).map(BigDecimal::new).orElse(BigDecimal.ZERO);
        BigDecimal subTotalAmount = Optional.ofNullable(subtractMap).map(m -> m.get("totalAmount")).map(Object::toString).map(BigDecimal::new).orElse(BigDecimal.ZERO);

        JSONObject obj = new JSONObject();
        obj.put("totalCount", countMap.getOrDefault("totalCount", 0));
        obj.put("totalAmount", countMap.getOrDefault("totalAmount", 0));
        obj.put("addTotalCount", addMap.getOrDefault("totalCount", 0));
        obj.put("addTotalAmount", addTotalAmount);
        obj.put("subTotalCount", subtractMap.getOrDefault("totalCount", 0));
        obj.put("subTotalAmount", subTotalAmount);
        obj.put("surplusTotalAmount", addTotalAmount.add(subTotalAmount));
        return ResponseEntity.ok(PayResponse.buildSuccess(obj));
    }

    @RequestMapping("/detail")
    @MethodLog(remark = "订单详情")
    public ResponseEntity detail(Long id) {
        CodeAccountHistory history = rpcService.codeAccountHistoryService.getById(id);
        if (history == null) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.CODE_ORDER_NOT_EXIST));
        }
        return ResponseEntity.ok(PayResponse.buildSuccess(history));
    }

}
